package com.ywu.standard.gateway.service;

import com.ywu.api.spi.dto.ApiRouterDto;
import com.ywu.common.result.CommonResult;
import com.ywu.standard.gateway.dto.GatewayRouteRedisDto;
import com.ywu.standard.gateway.redis.RedisUtil;
import com.ywu.standard.gateway.service.handler.GatewayServiceHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName GatewayRouterService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/1/28 17:56
 * @Version 1.0
 **/
@Service
@Slf4j
public class GatewayRouterService {
    @Autowired
    private GatewayServiceHandler gatewayServiceHandler;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * @Author GroundDemo
     * @Description 刷新路由
     * @Date 21:38 2024/2/15
     * @Param []
     * @return com.ywu.blog.common.result.CommonResult<java.lang.Object>
     **/
    public CommonResult<Object> refreshRoute() {
        // 删除缓存中的内容
        gatewayServiceHandler.deleteRedisCache();
        return gatewayServiceHandler.loadRouteConfig();
    }

    /**
     * @Author GroundDemo
     * @Description 删除路由
     * @Date 21:36 2024/2/15
     * @Param [gatewayRouterDto]
     * @return com.ywu.blog.common.result.CommonResult<java.lang.Object>
     **/
    public CommonResult<Object> deleteRoute(ApiRouterDto gatewayRouterDto) {
        gatewayServiceHandler.deleteRoute(gatewayRouterDto.getCode());
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 查询API的redis缓存
    * @Date 22:03 2024/2/29
    * @Param [gatewayRouterDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<List<GatewayRouteRedisDto>> apiRouteRedisCacheInfo(ApiRouterDto gatewayRouterDto) {
        List<RouteDefinition> allGatewayRoutes = redisUtil.getAllGatewayRoutes();
        // 遍历allGatewayRoutes过滤出所有符合条件的redis数据
        List<GatewayRouteRedisDto> res = new ArrayList<>();
        for (RouteDefinition definition : allGatewayRoutes) {
            GatewayRouteRedisDto gatewayRouteRedisDto = new GatewayRouteRedisDto();
            gatewayRouteRedisDto.setId(definition.getId());
            gatewayRouteRedisDto.setFilters(definition.getFilters().toString());
            gatewayRouteRedisDto.setPredicates(definition.getPredicates().toString());
            gatewayRouteRedisDto.setUri(definition.getUri().toString());
            res.add(gatewayRouteRedisDto);
        }
        return CommonResult.success(res);
    }

    /**
    * @Author GroundDemo
    * @Description 重新加载路由信息
    * @Date 10:32 2024/3/10
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> reloadRoute() {
        return gatewayServiceHandler.loadRouteConfig();
    }

    /**
    * @Author GroundDemo
    * @Description 添加路由
    * @Date 22:06 2025/3/20
    * @Param [gatewayRouterDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> addApiRouteCacheInfo(ApiRouterDto gatewayRouterDto) {
        return gatewayServiceHandler.addApiRoute(gatewayRouterDto);
    }
}